package cn.iocoder.yudao.module.product.controller.admin.categoryPlatform;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.categoryPlatform.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.categoryPlatform.CategoryPlatformDO;
import cn.iocoder.yudao.module.product.service.categoryPlatform.CategoryPlatformService;

@Tag(name = "管理后台 - 平台商品分类")
@RestController
@RequestMapping("/product/category-platform")
@Validated
public class CategoryPlatformController {

    @Resource
    private CategoryPlatformService categoryPlatformService;

    @PostMapping("/create")
    @Operation(summary = "创建平台商品分类")
    @PreAuthorize("@ss.hasPermission('product:category-platform:create')")
    public CommonResult<Long> createCategoryPlatform(@Valid @RequestBody CategoryPlatformSaveReqVO createReqVO) {
        return success(categoryPlatformService.createCategoryPlatform(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台商品分类")
    @PreAuthorize("@ss.hasPermission('product:category-platform:update')")
    public CommonResult<Boolean> updateCategoryPlatform(@Valid @RequestBody CategoryPlatformSaveReqVO updateReqVO) {
        categoryPlatformService.updateCategoryPlatform(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台商品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:category-platform:delete')")
    public CommonResult<Boolean> deleteCategoryPlatform(@RequestParam("id") Long id) {
        categoryPlatformService.deleteCategoryPlatform(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得平台商品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:category-platform:query')")
    public CommonResult<CategoryPlatformRespVO> getCategoryPlatform(@RequestParam("id") Long id) {
        CategoryPlatformDO categoryPlatform = categoryPlatformService.getCategoryPlatform(id);
        return success(BeanUtils.toBean(categoryPlatform, CategoryPlatformRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得平台商品分类列表")
    @PreAuthorize("@ss.hasPermission('product:category-platform:query')")
    public CommonResult<List<CategoryPlatformRespVO>> getCategoryPlatformList(@Valid CategoryPlatformListReqVO listReqVO) {
        List<CategoryPlatformDO> list = categoryPlatformService.getCategoryPlatformList(listReqVO);
        return success(BeanUtils.toBean(list, CategoryPlatformRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台商品分类 Excel")
    @PreAuthorize("@ss.hasPermission('product:category-platform:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCategoryPlatformExcel(@Valid CategoryPlatformListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<CategoryPlatformDO> list = categoryPlatformService.getCategoryPlatformList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "平台商品分类.xls", "数据", CategoryPlatformRespVO.class,
                        BeanUtils.toBean(list, CategoryPlatformRespVO.class));
    }

}